package com.knowif.backend.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.knowif.backend.dto.TutorPublishDto;
import com.knowif.dto.TutorInfoDto;
import com.knowif.pojo.TutorInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.knowif.backend.service.TutorService;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;

@RestController
@RequestMapping("/wechat/tutor")
@Slf4j
public class TutorController {

	@Autowired
	private TutorService tutorService;

	/**
	 * 导师筛选
	 * @param searchCondition
	 * @return
	 */
	@PostMapping("/screen")
	public RestBean<?> screen(@RequestBody SearchCondition searchCondition){
		PageInfo<TutorInfo> pageInfo = tutorService.screen(searchCondition);
		return RestBean.build(RestBean.success_code, "", pageInfo);
	}

	@PostMapping("/self/publish")
	public RestBean selfPublish(@RequestBody TutorPublishDto tutorPublishDto){
		log.info("自我发布" + JSONObject.toJSONString(tutorPublishDto));
		return tutorService.selfPublish(tutorPublishDto);
	}

	/**
	 * 修改名字
	 * @param userId
	 * @param fullName
	 * @return
	 */
	@PostMapping("/self/updateFullName")
	public RestBean updateFullName(@RequestParam("userId")int userId,
								   @RequestParam("fullName")String fullName){
		return tutorService.updateFullName(userId, fullName);
	}

	/**
	 * 修改自我展示
	 * @param userId
	 * @param selfShow
	 * @return
	 */
	@PostMapping("/self/updateSelfShow")
	public RestBean updateSelfShow(@RequestParam("userId")int userId,
								   @RequestParam("selfShow")String selfShow){
		return tutorService.updateSelfShow(userId, selfShow);
	}

	//更新导师信息
	@PostMapping("/update")
	public RestBean<?> fuzzySearch(@RequestBody TutorInfo tutorInfo) {
		if(tutorInfo == null){
			return RestBean.buildError("参数错误");
		}
		try {
			return tutorService.update(tutorInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	

	//模糊查询导师信息
	@PostMapping("/fuzzySearch")
	public RestBean<?> fuzzySearch(@RequestBody SearchCondition searchCondition) {
		if(searchCondition == null){
			return RestBean.buildError("请输入模糊查询内容");
		}
		try {
			return tutorService.fuzzySearch(searchCondition);
		} catch (Exception e) {
			e.printStackTrace();
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}

    /**
     * 查询导师详情
     * @param tutorId
     * @return
     */
	@PostMapping("/selectTutorInfo")
	public RestBean<?> selectTutorInfo(Integer tutorId,Integer userId){
		try{
			return tutorService.selectTutorInfo(tutorId,userId);
		}catch(Exception e){
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}

	/**
	 * 查询导师详情by用户id
	 * @param userId
	 * @return
	 */
	@PostMapping("/selectTutorInfoByUserId")
	public RestBean<?> selectTutorInfoByUserId(Integer userId){
		if(userId == null){
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
		return tutorService.selectTutorInfoByUserId(userId);
	}
	
	/**
	 * @author: xingyao 
	 * @Description:  通过帮助人数进行排序查询
	 * @param searchCondition
	 * @return
	 */
	@PostMapping("/selectTutorByHelpNum")
	public RestBean<?> selectTutorByHelpNum(@RequestBody SearchCondition searchCondition){
		try {
			return tutorService.selectOrderByHelpNum(searchCondition);
		} catch (Exception e) {
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	
	/**
	 * @author: xingyao 
	 * @Description:  通过总分进行排序查询
	 * @param searchCondition
	 * @return
	 */
	@PostMapping("/selectTutorBytotalScore")
	public RestBean<?> selectTutorBytotalScore(@RequestBody SearchCondition searchCondition){
		try {
			return tutorService.selectOrderBytotalScore(searchCondition);
		} catch (Exception e) {
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	/**
	 * @author: xingyao 
	 * @Description:  通过导师的城市，学院名称，学校地址进行筛选，其中city:xxx;college:xxx;school_city:xxx
	 * @param searchCondition
	 * @return
	 */
	@PostMapping("/selectTutorByCondition")
	public RestBean<?> selectTutorByCondition(@RequestBody SearchCondition searchCondition){
		try {
			return tutorService.selectOrderByHelpNum(searchCondition);
		} catch (Exception e) {
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	@PostMapping("/selectTutorByItem")
	public RestBean<?> selectTutorByItem(@RequestBody SearchCondition searchCondition){
		if(searchCondition == null){
			return RestBean.buildError("请输入要查找的分类id");
		}
		try {
			return tutorService.selectTutorByItem(searchCondition);
		} catch (Exception e) {
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	
	
}
